#include "Comm.h"

QSerialPort *serialPort = new QSerialPort;                            // 定义全局的串口对象

static DeerRxHandle *deerRxHandle;
static QTimer* timer;
Comm uartComm;

Comm::Comm(void){
    this->State = Comm_NotLink;
    qDebug() << "确实调用了构造方法 this->State:" << this->State;
}

void Comm::TimOutFun(void)
{
    deerRxHandle->run();
}

uint8_t Comm::Connect(QLabel *label_link, QPushButton *pushButton) {

    if (pushButton->text() == "连接") {
        /* 串口设置 */
        serialPort->setPortName(this->com);
        serialPort->setBaudRate(this->speed);
        serialPort->setDataBits(QSerialPort::Data8);
        serialPort->setStopBits(QSerialPort::OneStop);
        serialPort->setParity(QSerialPort::NoParity);

        /* 打开串口提示框 */
        if (true == serialPort->open(QIODevice::ReadWrite))
        {
            uartComm.State = Comm_Read;

            //创建定时器对象
            timer = new QTimer();

            qDebug() << "确实启动了定时器";
            timer->start(1);
            connect(timer, SIGNAL(timeout()), this, SLOT(TimOutFun()));

            label_link->setText("连接成功！");
            pushButton->setText("断开");
            return 0;
        } else {
            uartComm.State = Comm_NotLink;
            label_link->setText("连接失败");
            return 1;
        }
    } else if (pushButton->text() == "断开") {
        serialPort->close();
        uartComm.State = Comm_NotLink;
        pushButton->setText("连接");

        timer->stop();

        delete timer;

        label_link->setText("断开成功！");

        return 2;
    }

    return 0xff;
}

void Comm::updata(QComboBox *hlist)
{
    QStringList serialNamePort;

    /* 搜索所有可用串口 */
    foreach (const QSerialPortInfo &inf0, QSerialPortInfo::availablePorts()) {
        serialNamePort<<inf0.portName();
    }

    hlist->clear();
    hlist->addItems(serialNamePort);
}

uint8_t Comm::tx_data(uint8_t fun, uint8_t *data, uint8_t datalen){
    if (uartComm.State == Comm_NotLink) {
        return Comm_NotLink;
    }

    uint8_t SendBuff[64];
    uint8_t len = 0;

    /* 帧头 */
    SendBuff[len++] = 0xFD;
    SendBuff[len++] = 0xDF;

    /* 总长度 先占位 */
    SendBuff[len++] = 0;

    /* 功能帧 */
    SendBuff[len++] = fun;

    /* 数据 */
    for (int max = (datalen + len), now = len; len < max; len++) {
        SendBuff[len] = data[len - now];
    }

    /* 获取实际长度 */
    SendBuff[2] = len;

    /* 校验位 */
    SendBuff[len] = 0;
    for (int i = 0; i < len; i++) {
        SendBuff[len] += SendBuff[i];
    }

    if (uartComm.State == Comm_Read) {
        uartComm.State = Comm_Busy;
        serialPort->write((char *)SendBuff, len+1);
        uartComm.State = Comm_Read;
        return Comm_OK;
    }

    return uartComm.State;
}

uint8_t Comm::tx_uint16(uint8_t fun, uint16_t data)
{
    if (uartComm.State == Comm_NotLink) {
        return Comm_NotLink;
    }

    uint8_t txdata[2];
    txdata[0] = (uint8_t)(data >> 8);
    txdata[1] = (uint8_t)data;

    return this->tx_data(fun, txdata, 2);
}

uint8_t Comm::tx_MotorCtrl(uint8_t Motor, uint8_t Mode, float data)
{
    union {
        float f;
        uint8_t u8[4];
    } temp;

    temp.f = data;

    uint8_t txdata[5];

    txdata[1] = Motor;  //电机
    txdata[1] = temp.u8[0];
    txdata[2] = temp.u8[1];
    txdata[3] = temp.u8[2];
    txdata[4] = temp.u8[3];

    return this->tx_data(Mode, txdata, 5);
}
